package com.zx.platform.service.impl;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.zx.map.utils.CommonResponseUtils;
import com.zx.map.utils.SUConstants;
import com.zx.platform.utils.DesUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DataAccessException;
import org.springframework.stereotype.Component;

import com.zx.platform.dao.OperatorInfoDao;
import com.zx.platform.dao.model.Menus;
import com.zx.platform.dao.model.OperatorInfo;
import com.zx.platform.dao.model.OperatorInfoExample;
import com.zx.platform.dao.model.OperatorRoles;
import com.zx.platform.dao.model.OperatorRolesTemp;
import com.zx.platform.service.MenusService;
import com.zx.platform.service.OperatorInfoService;
import com.zx.platform.service.RolesService;
import com.zx.platform.service.domain.OperatorInfoDomain;
import com.zx.platform.service.model.MenusModel;
import com.zx.platform.service.model.OperatorInfoModel;
import com.zx.platform.service.model.OperatorRolesModel;
import com.zx.platform.service.model.RolesModel;
import com.zx.platform.utils.DateUtils;
import com.zx.platform.utils.StrUtils;

/**
 * Created by IntelliJ IDEA. User: Administrator Date: 14-3-1 Time: 上午11:07 To
 * change this template use File | Settings | File Templates.
 */
@Component("operatorInfoService")
public class OperatorInfoServiceImpl implements OperatorInfoService {

	@Autowired
	private OperatorInfoDao dao;
	
	@Autowired
	private RolesService rolesService;
	
	@Autowired
	private MenusService menusService;

	/**
	 * 得到所有数据列表
	 */
	public List getAllList() throws DataAccessException {

		List<OperatorInfo> list = dao.getListByExample(null);
		List<OperatorInfoModel> modelList = new ArrayList<OperatorInfoModel>();
		for (OperatorInfo o : list) {
			OperatorInfoModel temp = new OperatorInfoModel();
			BeanUtils.copyProperties(o, temp);
			modelList.add(temp);
		}
		return modelList;

	}

	/**
	 * 根据单Key查找Model
	 */
	public OperatorInfoModel getModelByKey(String key) throws DataAccessException {
		OperatorInfo o = dao.selectByPrimaryKey(key);
		OperatorInfoModel model = new OperatorInfoModel();
		BeanUtils.copyProperties(o, model);
		return model;
	}

	/**
	 * 根据Model查找列表
	 */
	public List<OperatorInfoModel> getListByModel(OperatorInfoModel model) throws DataAccessException {
		OperatorInfoExample example = new OperatorInfoExample();
		com.zx.platform.dao.model.OperatorInfoExample.Criteria criteria = example.createCriteria();
		if (StringUtils.isNotBlank(model.getOperatorName())) {
			criteria.andOperatorNameLike('%'+model.getOperatorName()+'%');
		}
		List<OperatorInfo> list = dao.getListByExample(example);
		List<OperatorInfoModel> modelList = new ArrayList<OperatorInfoModel>();
		for (OperatorInfo o : list) {
			OperatorInfoModel temp = new OperatorInfoModel();
			BeanUtils.copyProperties(o, temp);
			modelList.add(temp);
		}
		return modelList;
	}
	/**
	 * 密码校验
	 */
	public List passOrNotByModel(OperatorInfoModel model) throws DataAccessException {
		OperatorInfoExample example = new OperatorInfoExample();
		com.zx.platform.dao.model.OperatorInfoExample.Criteria criteria = example.createCriteria();
		criteria.andLoginNameEqualTo(model.getLoginName());
		criteria.andLoginPasswordEqualTo(model.getLoginPassword());
		List<OperatorInfo> list = dao.getListByExample(example);
		List<OperatorInfoModel> modelList = new ArrayList<OperatorInfoModel>();
		for (OperatorInfo o : list) {
			OperatorInfoModel temp = new OperatorInfoModel();
			BeanUtils.copyProperties(o, temp);
			modelList.add(temp);
		}
		return modelList;
	}

	/**
	 * 新增
	 */
	public String add(OperatorInfoModel model) throws DataAccessException {
		OperatorInfo temp = new OperatorInfo();
		BeanUtils.copyProperties(model, temp);
		String key = dao.add(temp);
		return key;
	}

	/**
	 * 修改
	 */
	public String update(OperatorInfoModel model) throws DataAccessException {
		OperatorInfo temp = new OperatorInfo();
		BeanUtils.copyProperties(model, temp);
		dao.update(temp);
		return model.getOperatorId();
	}

	/**
	 * 删除
	 */
	public void delete(OperatorInfoModel model) throws DataAccessException {
		OperatorInfo temp = new OperatorInfo();
		BeanUtils.copyProperties(model, temp);
		dao.delete(temp);
	}

	/**
	 * 分页查询
	 * 
	 * @param pageSize
	 * @param pageNum
	 * @param menuModel
	 * @return
	 * @throws DataAccessException
	 */
	public Map getPageByDomain(int pageSize, int pageNum, OperatorInfoDomain domain)
			throws DataAccessException {

		Map<String, Object> map = new HashMap<String, Object>();
		OperatorInfoExample example = new OperatorInfoExample();
		com.zx.platform.dao.model.OperatorInfoExample.Criteria criteria = example.createCriteria();
		if (StringUtils.isNotBlank(domain.getOperatorName())) {
			criteria.andOperatorNameLike('%'+domain.getOperatorName()+'%');
		}
		if (StringUtils.isNotBlank(domain.getStatus())) {
			criteria.andStatusEqualTo(domain.getStatus());
		}
		//增加操作员类型过滤
		if (StringUtils.isNotBlank(domain.getOperatorType())) {
			criteria.andOperatorTypeEqualTo(domain.getOperatorType());
		}
		if(domain.getBegindate()!=null){
			criteria.andCreateDateGreaterThanOrEqualTo(domain.getBegindate());
		}
		Date enddate=null;
		SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd"); 
		if(domain.getEnddate()!=null){
			enddate=domain.getEnddate();
			String enddateStr=dateFormat.format(enddate);
			enddateStr=enddateStr+" 23:59:59";
			criteria.andCreateDateLessThanOrEqualTo(DateUtils.strToDate(enddateStr,"yyyy-MM-dd HH:mm:ss"));
		}
		//排序add by wenjw 2015.6.2
		example.setOrderByClause("create_Date desc");
		int pageBegin = (pageNum - 1) * pageSize;
		List<OperatorInfo> list = dao.getPageByExample(pageSize, pageBegin, example);
		List<OperatorInfoModel> menuModelList = new ArrayList<OperatorInfoModel>();
		for (OperatorInfo o : list) {
			OperatorInfoModel temp = new OperatorInfoModel();
			BeanUtils.copyProperties(o, temp);
			List<RolesModel> roleList = this.getRoleListByOperatorId(o.getOperatorId());
			for(RolesModel rolesModel :roleList){
				temp.setRoleNames(StrUtils.contract(temp.getRoleNames(), rolesModel.getRoleName(), ";"));
			}
			menuModelList.add(temp);
		}
		int count = dao.countByExample(example);
		map.put("count", count);
		map.put("list", menuModelList);
		return map;
	}

	/**
	 * 检验唯一性
	 */
	public List<OperatorInfoModel> checkSingle(OperatorInfoModel model)
			throws DataAccessException {
		OperatorInfoExample example = new OperatorInfoExample();
		com.zx.platform.dao.model.OperatorInfoExample.Criteria criteria = example.createCriteria();
		criteria.andLoginNameEqualTo(model.getLoginName());
		if(StrUtils.isNotEmpty(model.getOperatorId())){
			criteria.andOperatorIdNotEqualTo(model.getOperatorId());
		}
		List<OperatorInfo> list = dao.getListByExample(example);
		List<OperatorInfoModel> modelList = new ArrayList<OperatorInfoModel>();
		for (OperatorInfo o : list) {
			OperatorInfoModel temp = new OperatorInfoModel();
			BeanUtils.copyProperties(o, temp);
			modelList.add(temp);
		}
		return modelList;
	}

	/**
	 * 
	 * 设置角色
	 * 是先删除再新增
	 */
	public String setOperatorRoles(OperatorRolesModel model)
			throws DataAccessException {
		OperatorRolesTemp temp = new OperatorRolesTemp();
		BeanUtils.copyProperties(model, temp);
		return dao.setOperatorRoles(temp);
	}
	/**
	 * 根据操作员id获得角色列表
	 * 
	 */
	public List<RolesModel> getRoleListByOperatorId(String operatorId)
			throws DataAccessException {
		/*List<RolesModel> roleList = new ArrayList();
		List<OperatorRoles> OperatorRolesList = dao.getOperatorRoles(operatorId);
		for(OperatorRoles operatorRoles:OperatorRolesList){
			Integer roleId = operatorRoles.getRoleId();
			RolesModel rolesModel = rolesService.getModelByKey(roleId);
			roleList.add(rolesModel);
		}*/
		List<RolesModel> roleList = new ArrayList();
		OperatorInfo operatorInfo = dao.selectByPrimaryKey(operatorId);
		Integer roleId = operatorInfo.getRoleId();
		RolesModel rolesModel = rolesService.getModelByKey(roleId);
		roleList.add(rolesModel);
		return roleList;
	}

	/**
	 *根据登录名查找操作员 
	 */
	public OperatorInfoModel getModelByAgentInfo(OperatorInfoModel model)
			throws DataAccessException {
		OperatorInfoExample example = new OperatorInfoExample();
		com.zx.platform.dao.model.OperatorInfoExample.Criteria criteria = example.createCriteria();
		if (StringUtils.isNotBlank(model.getLoginName())) {
			criteria.andLoginNameEqualTo(model.getLoginName());
		}
		List<OperatorInfo> list = dao.getListByExample(example);
		List<OperatorInfoModel> modelList = new ArrayList<OperatorInfoModel>();
		for (OperatorInfo o : list) {
			OperatorInfoModel temp = new OperatorInfoModel();
			BeanUtils.copyProperties(o, temp);
			modelList.add(temp);
		}
		if(modelList!=null&&modelList.size()>0){
			return modelList.get(0);
		}
		return null;
	}
	/**
	 * 根据操作员id获得拥有的权限菜单
	 */
	public List<MenusModel> getMenuListByOperatorId(String operatorId)
			throws DataAccessException {
		List<RolesModel> roleList = this.getRoleListByOperatorId(operatorId);
		List<Menus> operatorMenuList = new ArrayList();
		if(roleList!=null&&roleList.size()>0){
			for(RolesModel r:roleList){
				List<Menus> menusList = menusService.getMenusByRoleId(r.getRoleId());
				if(menusList!=null&&menusList.size()>0){
					operatorMenuList.addAll(menusList);
				}
			}
		}
		List<MenusModel> modelList = new ArrayList();
		for(Menus o :operatorMenuList){
			MenusModel temp = new MenusModel();
			BeanUtils.copyProperties(o, temp);
			modelList.add(temp);
		}
		return modelList;
	}

	@Override
	public Map resetPwdBySysAccount(String sysAccount) throws Exception{
		OperatorInfoModel operatorInfoModel = new OperatorInfoModel();
		operatorInfoModel.setLoginName(sysAccount);
		List<OperatorInfoModel> operatorInfoModelList = checkSingle(operatorInfoModel);
		if (operatorInfoModelList != null
				&& operatorInfoModelList.size() > 0) {
			OperatorInfoModel singleOperatorInfoModel = operatorInfoModelList
					.get(0);
			// 重置密码为默认密码
			singleOperatorInfoModel.setLoginPassword(DesUtil.encrypt(
					SUConstants.DEFAULT_PWD, null));
			update(singleOperatorInfoModel);

			return CommonResponseUtils.successResponse("重置成功!");
		} else {
			return CommonResponseUtils.successResponse("用户不存在!");
		}
	}
}
